package com.last.listener;

import com.api.houseDetail.HouseDetailApi;
import com.last.entity.RoomInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

@Service
@RocketMQMessageListener(consumerGroup = "homestay-page-consumer",
    topic = "house-page",selectorExpression = "page-tag",
    consumeMode = ConsumeMode.CONCURRENTLY,
    messageModel = MessageModel.BROADCASTING)
public class PageListener implements RocketMQListener<Integer> {

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    @Autowired
    private HouseDetailApi houseDetailApi;

    @Override
    public void onMessage(Integer roomId) {
        RoomInfo roomInfoDetail = houseDetailApi.getRoomInfoDetail(roomId);

        try {
//            System.out.println("roomId:"+roomId);
            //生成静态化页面
            Configuration configuration = freeMarkerConfig.getConfiguration();
            Template template = configuration.getTemplate("houseDetail.ftl");

            Map data = new HashMap();
            data.put("roomDetail", roomInfoDetail);
            data.put("merchant",roomInfoDetail.getMerchant());
            data.put("houseName", roomInfoDetail.getHouseName());

            //创建输出流(Writer)对象
            Writer out = new FileWriter("D:\\Program Files\\nginx-1.18.0\\minsu\\staticPage\\"+roomId+".html");
            //输出
            template.process(data,out);
            //关闭
            out.close();


        }catch (Exception e){
            e.printStackTrace();
        }

    }

}